package pl.edu.agh.portal.application.portlets;

import java.io.IOException;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.utils.PortletsUtils;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeUtils;
import pl.edu.agh.thesis.webservices.project.ProjectManagerSkeleton;
import pl.edu.agh.thesis.webservices.project.client.ProjectManagerStub;
import pl.edu.agh.thesis.webservices.project.client.ProjectUtils;

public class ProjectListPortlet extends ThesisGenericPortlet {

	private static final String NORMAL_VIEW = "/project_list_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
		
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {			
											
		int employeeId = EmployeeUtils.validate(PortletsUtils.temp_session);
			
		ProjectManagerStub.Project[] projectArray = ProjectUtils.getAll(PortletsUtils.temp_session, String.valueOf(employeeId), ProjectManagerSkeleton.USER_ID_RESTRICTION);
			
		request.setAttribute(PortletsUtils.USER_PROJECTS_KEY, projectArray);
		normalView.include(request, response);		
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
		String remove = request.getParameter(PortletsUtils.BUTTON_REMOVE);
		String details = request.getParameter(PortletsUtils.BUTTON_DETAILS);
				
		if (remove != null) {
			response.sendRedirect("/portal/portal/project_remove.psml");
			PortletsUtils.vault.put("projectId", remove);
		} else if (details != null) {			
			response.sendRedirect("/portal/portal/project_details.psml");
			PortletsUtils.vault.put("projectId", details);
		}		
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}
		
}

